﻿Imports System.Data
Imports VSModel
Imports Cls_Common
Imports SecurityService

Partial Class Control_MSDS_Section08
    Inherits System.Web.UI.UserControl
    Private Shared ReadOnly log As log4net.ILog = log4net.LogManager.GetLogger(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType)
#Region "Sub and Function "
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        'Label
        Translate()
        If Not IsPostBack Then
            Dim script As ScriptManager = ScriptManager.GetCurrent(Me.Page)
            If Not script Is Nothing AndAlso script.IsInAsyncPostBack Then
                ScriptManager.RegisterStartupScript(Me.Page, Me.Page.GetType, "duyjs", "ajaxJquery()", True)
            Else
                Me.Page.ClientScript.RegisterStartupScript(Me.Page.GetType(), "duyjs", String.Concat("Sys.Application.add_load(function(){", "ajaxJquery()", "});"), True)
            End If

            hidMSDSID.Value = IIf(IsNothing(Request.QueryString("Id")), 0, CInt(Request.QueryString("Id")))
            ShowData()
        End If
    End Sub
    Protected Sub Translate()
        lblSection08Header.Text = getTranslate("lblSection08Header", Session("LanguageId"), lblSection08Header.Text)
        lblS811.Text = getTranslate("lblS811", Session("LanguageId"), lblS811.Text)
        lblS821.Text = getTranslate("lblS821", Session("LanguageId"), lblS821.Text)
        lblS831.Text = getTranslate("lblS831", Session("LanguageId"), lblS831.Text)
        lblS841.Text = getTranslate("lblS841", Session("LanguageId"), lblS841.Text)
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            'Tiêu đề doanh nghiệp
            Dim lang = CInt(Session("LanguageId"))
            Dim Id As Integer = hidMSDSID.Value
            Dim p As MSDSSection08 = (From a In data.MSDSSection08 Where a.MSDSId = Id And a.LanguageId = lang).SingleOrDefault
            If Not IsNothing(p) Then
                hidID.Value = p.Section08Id

                'Content
                txtS811.Text = IIf(IsNothing(p.S811) = True, "", p.S811)
                'txtS821.Text = IIf(IsNothing(p.S821) = True, "", p.S821)
                Dim strS821 = ""
                If Not IsNothing(p.S821) Then
                    Dim arrS821() As String = p.S821.Split(Str_Symbol_Group)
                    For index As Integer = 0 To arrS821.Count - 2
                        strS821 = strS821 + arrS821(index) & Environment.NewLine
                    Next
                    strS821 = strS821 + arrS821(arrS821.Count - 1)
                End If
                txtS821.Text = strS821
                txtS831.Text = IIf(IsNothing(p.S831) = True, "", p.S831)
                txtS841.Text = IIf(IsNothing(p.S841) = True, "", p.S841)
            End If
          
        End Using
    End Sub
#End Region
#Region "Event for control "
    Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Using data As New VSEntities
            Try
                Dim p As MSDSSection08
                p = (From q In data.MSDSSection08
                Where q.Section08Id = hidID.Value
                  ).FirstOrDefault()
                Dim _isNew = True
                If Not p Is Nothing Then
                    _isNew = False
                Else
                    p = New MSDSSection08
                End If
                With p
                    .S811 = txtS811.Text.Trim
                    '.S821 = txtS821.Text.Trim
                    Dim strResult = ""
                    For Each item In ReadAllLines(txtS821.Text.Trim())
                        If Not String.Equals(item, "") Then
                            strResult = strResult & item & Str_Symbol_Group
                        End If
                    Next
                    'Cắt đi ký tự "Str_Symbol_Group" cuối cùng.
                    If strResult.Length > 0 Then
                        .S821 = strResult.Substring(0, strResult.Length - Str_Symbol_Group.Length)
                    Else
                        .S821 = Nothing
                    End If
                    .S831 = txtS831.Text.Trim()
                    .S841 = txtS841.Text.Trim()
                    .LanguageId = Session("LanguageId")
                    .MSDSId = hidMSDSID.Value
                End With
                If _isNew Then
                    p.Created = Now
                    p.Creator = Session("UserName")
                    data.MSDSSection08.AddObject(p)
                Else
                    p.Modified = Now
                    p.Modifier = Session("UserName")
                End If
                data.SaveChanges()

                Insert_App_Log(If(_isNew, "Insert", "Update") & "  MSDS Section08:" & hidMSDSID.Value & "", Function_Name.MSDSOriginal, If(_isNew, Audit_Type.Create, Audit_Type.Edit), Request.ServerVariables("REMOTE_ADDR"), Session("UserName"))
                Excute_Javascript("AlertboxRedirect('Lưu dữ liệu thành công.','../../Page/MSDS/Section09.aspx?Id=" & hidMSDSID.Value & "');", Me.Page, True)
            Catch ex As Exception
                log4net.Config.XmlConfigurator.Configure()
                log.Error("Error error " & AddTabSpace(1) & Session("Username") & AddTabSpace(1) & "IP:" & GetIPAddress(), ex)
                Excute_Javascript("Alertbox('Cập nhật thất bại.');", Me.Page, True)
            Finally
                GC.Collect()
            End Try
        End Using
    End Sub
    Protected Sub btnPrevious_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnPrevious.Click
        Response.Redirect("../../Page/MSDS/Section07.aspx?Id=" & hidMSDSID.Value)
    End Sub
#End Region
End Class
